package com.sky.controller.user;

import com.ejlchina.searcher.SearchResult;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import com.sky.web.controller.BaseController;
import com.sky.web.domain.AjaxResult;
import com.sky.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author tzl
 * @Date 2023-07-26 15:51:29  星期三
 * @Description: 用户订单模块接口
 */
@Api(tags = "用户订单模块接口")
@Slf4j
@RestController("userOrderController")
@RequestMapping("/user/order")
public class OrderController extends BaseController {


    @Autowired
    private OrderService orderService;

    /**
     * 历史订单查询
     * @param status   订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public TableDataInfo page(Integer status) {
        SearchResult<OrderVO> result = orderService.selectHistoryOrders(status);
        return getDataTable(result);
    }


    /**
     * 查询订单详情
     *
     * @param id 订单id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public AjaxResult details(@PathVariable("id") Long id) {
        OrderVO orderVO = orderService.selectOrderVOById(id);
        return AjaxResult.success(orderVO);
    }


    /**
     * 用户下单
     * @param dto 订单信息
     * @return
     */
    @ApiOperation("用户下单接口")
    @PostMapping("/submit")
    public AjaxResult submitOrder(@RequestBody OrdersSubmitDTO dto){
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(dto);
        return AjaxResult.success(orderSubmitVO);
    }



    /**
     * 订单支付
     *
     * @param dto 订单信息
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public AjaxResult payment(@RequestBody OrdersPaymentDTO dto) throws Exception {
        log.info("订单支付：{}", dto);
        OrderPaymentVO orderPaymentVO = orderService.payment(dto);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return AjaxResult.success(orderPaymentVO);
    }

    /**
     * 用户取消订单
     *
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public AjaxResult cancel(@PathVariable("id") Long id) {
        return toAjax(orderService.userCancelById(id));
    }

    /**
     * 再来一单
     * @param id 订单id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public AjaxResult repetition(@PathVariable("id") Long id) {
        return toAjax(orderService.repetition(id));
    }

    /**
     * 客户催单
     * @param id 订单id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public AjaxResult reminder(@PathVariable("id") Long id){
        orderService.reminder(id);
        return AjaxResult.success();
    }
}
